package com.aaa.mythread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class MyThread12 {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
       // 定义一下线程

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"即将执行");

                try {
                    // 休眠
//                    Thread.sleep(3000); // 3个小时
                   // Thread.sleep(3*60*60*1000);
                    TimeUnit.HOURS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

                // Thread.currentThread().getName()  默认的线程的名字是:Thread-0  起的名字 是什么 获取的名字就是什么
                System.out.println(Thread.currentThread().getName()+"执行的时间是:"+System.currentTimeMillis());

            }
        };

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"即将执行");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName()+"执行的时间是:"+System.currentTimeMillis());

            }
        };

        Runnable runnable3 = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"即将执行");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName()+"执行的时间是:"+System.currentTimeMillis());

            }
        };

        Thread thread = new Thread(runnable,"线程的名字1");
        Thread thread2 = new Thread(runnable2,"线程的名字2");
        Thread thread3 = new Thread(runnable3,"线程的名字3");

        thread.start();
        thread2.start();
        thread3.start();


    }


}
